import java.util.Arrays;

/**
 * @Author: Lenovo
 * @Date: 2024/6/9 10:02
 * @description:
 */
public class MyArrayList implements IList{
    public int[]array;
    public int useSize;
    public final int DEFAULT_CAPACITY=10;
    public MyArrayList(){
        this.array=new int[DEFAULT_CAPACITY];
    }
    @Override
    public void add(int data) {
        if(isFull()){
            grow();
        }
        this.array[this.useSize]=data;
        this.useSize++;
    }
    //扩容操作
    private void grow(){
        this.array= Arrays.copyOf(this.array,2*this.array.length);
    }
    @Override
    public boolean isFull() {
        return this.array.length==this.useSize;
    }
    private boolean isEmpty(){
        return this.array.length==0;
    }
    private void CheckPos(int pos) throws PosIllegal{
        if(pos<0||pos>useSize){
            throw new PosIllegal("pos位置不合法！！！");
        }
    }
    @Override
    public void add(int pos, int data) {
        try {
            CheckPos(pos);
            if(isFull()){
                grow();
            }
            for (int i = this.useSize; i >pos ; i--) {
                array[i]=array[i-1];
            }
            this.array[pos]=data;
            this.useSize++;
        }catch (PosIllegal e){
            e.printStackTrace();
        }
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < this.useSize; i++) {
            if(array[i]==toFind){
                return true;
            }
        }
        return false;
    }
    private void CheckPos2(int pos) throws PosIllegal{
        if(pos<0||pos>=useSize){
            throw new PosIllegal("Pos位置不合法");
        }
    }
    private void CheckEmpty()throws EmptyExpection{
        if(isEmpty()){
            throw new EmptyExpection("数组为空");
        }
    }
    @Override
    public int get(int pos) {
        try {
            CheckPos2(pos);
            CheckEmpty();
            return array[pos];
        }catch (PosIllegal e){
            e.printStackTrace();
        }catch (EmptyExpection e){
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public void set(int pos, int value) {
        try {
            CheckPos2(pos);
            CheckEmpty();
           array[pos]=value;
        }catch (PosIllegal e){
            e.printStackTrace();
        }catch (EmptyExpection e){
            e.printStackTrace();
        }
    }

    @Override
    public void remove(int toRemove) {
        try{
            CheckEmpty();
            int pos=indexOf(toRemove);
            if(pos==-1){
                return;
            }
            for (int i = pos; i <this.useSize-1 ; i++) {
                array[i]=array[i+1];
            }
            this.useSize--;
        }catch (EmptyExpection e){
            e.printStackTrace();
        }
    }

    @Override
    public int size() {
        return this.useSize;
    }

    @Override
    public void clear() {
        /*
        如果是数组里面是引用数据类型，就需要这样子做了
        for(int i=0;i<useSize;i++){
            array[i]=null;
        }
         */
        this.useSize=0;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i=0;i<this.useSize;i++){
            if(array[i]==toFind){
                return i;
            }
        }
        return -1;
    }

    @Override
    public void display() {
        for(int i=0;i<this.useSize;i++){
            System.out.print(this.array[i]+" ");
        }
        System.out.println();
    }
}
